//
//  RatingView.m
//  MyMovie
//
//  Created by Weiwei on 16/2/26.
//  Copyright © 2016年 bever贝沃汇力 weiwei. All rights reserved.
//

#import "RatingView.h"
@interface  RatingView(){
    
    CGFloat _width;
    CGFloat _heigh;
    
    UIView* _grayView;
    UIView* _yellowView;
    
    
}


@end

@implementation RatingView
//如果代码创建可以使用
-(id)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        [self _creatStarView];
    }
    return self;
    
}


//如果加到xib文件里也可以使用
-(void)awakeFromNib{
    [super awakeFromNib];
    [self _creatStarView];
    
    
}
#pragma -mark封装星星的方法
-(void)_creatStarView{
    
    //1.首先获得两个星星图片
    UIImage* grayStar = [UIImage imageNamed:@"gray"];
    UIImage* yellowStar = [UIImage imageNamed:@"yellow"];
    //2.获取图片的大小，因为这两张应该是一样大的,找出一个作为代表
    //存放yellowView和grayView的长和宽
    _width = 5* grayStar.size.width;
    _heigh = grayStar.size.height;
    
    //3.创建一个灰色星星view，再创建一个黄色星星view
     _grayView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _width, _heigh)];
    _yellowView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _width, _heigh)];
    //4.平铺灰星星
    [_grayView setBackgroundColor:[UIColor colorWithPatternImage:grayStar]];
    [self addSubview:_grayView];
    
    //5.平铺黄色星星
    [_yellowView setBackgroundColor:[UIColor colorWithPatternImage:yellowStar]];
    [self addSubview:_yellowView];
    
#pragma -mark目标一 RatingView的宽控制星星试图的宽
    //1.算出RatingView的宽是星星试图的多少倍
   CGFloat scale  = self.frame.size.width/_width;
    //2.坐标系统的转换
    _grayView.transform  = CGAffineTransformScale(_grayView.transform, scale, scale);
    _yellowView.transform = CGAffineTransformScale(_yellowView.transform, scale, scale);
    
    
    //放大后位置偏移乱了,重新设置位置
    _grayView.frame = CGRectMake(0, 0, _grayView.frame.size.width, _grayView.frame.size.height);
    _yellowView.frame = CGRectMake(0, 0, _yellowView.frame.size.width, _yellowView.frame.size.height);
    
    
}
//如何设置评分后控制黄色星星的平铺大小
-(void)setRating:(float)rating{
    
//    //1.改变yellowView的大小
//    _yellowView.frame = CGRectMake(0, 0, _width*(rating/10.0), _heigh);
    
    //1.放大后修改黄色view的大小，按照rating值来
    _yellowView.frame = CGRectMake(0, 0, _grayView.frame.size.width * rating/10.0, _grayView.frame.size.height);
    
    
    
}

@end
